float fv[][] = new float[][]{{200,400},{300,200},{400,400}};
//float tv[][] = new float[][]{{0,200},{100,100},{200,200}};
float tv[][] = new float[][]{{100,100},{150,130},{200,100}};
int ftr[] = new int[]{0,1,2};//from triangle
int ttr[] = new int[]{0,1,2}; //to triangle
float frp[] = new float[2];//from point
float top[] = new float[2];//to point
float fc[] = new float[]{200,200};
float tc[] = new float[]{200,200};
float A,B,C;


void setup() {
  size(400,400);
}

void draw() {
  background(0,0,255);

  fill(0,255,0);
  triangle(tv[ttr[0]][0],tv[ttr[0]][1],tv[ttr[1]][0],tv[ttr[1]][1],tv[ttr[2]][0],tv[ttr[2]][1]);
  fill(255,0,0);
  triangle(fv[ftr[0]][0],fv[ftr[0]][1],fv[ftr[1]][0],fv[ftr[1]][1],fv[ftr[2]][0],fv[ftr[2]][1]);

  fill(0,255,0);
  rect(fc[0],fc[1],5,5);
  fill(0,255,0);
  rect(tc[0],tc[1],5,5);

  fill(255,0,0);
  rect(top[0],top[1],5,5);
  fill(0);
  rect(frp[0],frp[1],5,5);
}

void mousePressed() {
  frp[0] = mouseX;
  frp[1] = mouseY;
//  findColorPoint(frp,fv[ftr[0]],fv[ftr[1]],fv[ftr[2]],top,tv[ttr[0]],tv[ttr[1]],tv[ttr[2]]);
  findColorPoint(1,0,top,tv[ttr[0]],tv[ttr[1]],tv[ttr[2]]);
}

void findColorPoint(float s,float t,float toP[],float A2[],float B2[],float C2[]) {
  //find the new point according to new triangles coordinates
  float AB2[] = new float[] {B2[0]-A2[0],B2[1]-A2[1],0};
  float AC2[] = new float[] {C2[0]-A2[0],C2[1]-A2[1],0};
  toP[0] = A2[0] + s*AB2[0] + t*AC2[0];
  toP[1] = A2[1] + s*AB2[1] + t*AC2[1];
  println("top[0]"+top[0]+"**top[1]"+top[1]);
}

void findColorPoint(float frP[],float A1[],float B1[],float C1[],float toP[],float A2[],float B2[],float C2[]) {
  //1.find the needed vectors,lengths,degrees of the triangle
  float AB1[] = new float[] {B1[0]-A1[0],B1[1]-A1[1],0};
  float AB1L = DogaMakeUnitVector(AB1);
  float AC1[] = new float[] {C1[0]-A1[0],C1[1]-A1[1],0};
  float AC1L = DogaMakeUnitVector(AC1);
  float AP[] = new float[] {frP[0]-A1[0],frP[1]-A1[1],0};
  float APL = DogaMakeUnitVector(AP);
  float cosAlfa = DogaDotProduct(AB1,AP);
  float sinAlfa = sqrt(1-cosAlfa*cosAlfa);
  float cosBeta = DogaDotProduct(AP,AC1);
  float sinBeta = sqrt(1-cosBeta*cosBeta);
  //2.find the lengths needed for finding coefficients
  float d1 = APL*sinBeta;
  float AB1mid = d1/(sinAlfa*cosBeta+cosAlfa*sinBeta);//since sin(alfa+beta)=sin(alfa)*cos(beta)+cos(alfa)*sin(beta)
  float AC1mid = APL*cosBeta - sqrt(AB1mid*AB1mid-d1*d1);
  //3.find the coefficients
  float ABcoef=(AB1mid/AB1L);
  float ACcoef=(AC1mid/AC1L);
  //4.find the new point according to new triangles coordinates
  float AB2[] = new float[] {B2[0]-A2[0],B2[1]-A2[1],0};
  float AB2L = DogaMakeUnitVector(AB2);  
  float AC2[] = new float[] {C2[0]-A2[0],C2[1]-A2[1],0};
  float AC2L = DogaMakeUnitVector(AC2);
  toP[0] = A2[0] + ABcoef*AB2[0]*AB2L + ACcoef*AC2[0]*AC2L;
  toP[1] = A2[1] + ABcoef*AB2[1]*AB2L + ACcoef*AC2[1]*AC2L;
}

void findColorPointExp(float A1[],float B1[],float C1[],float A2[],float B2[],float C2[]) {
  fc = new float[]{(A1[0]+B1[0]+C1[0])/3,(A1[1]+B1[1]+C1[1])/3};//from triangles center
  tc = new float[]{(A2[0]+B2[0]+C2[0])/3,(A2[1]+B2[1]+C2[1])/3};//to triangles center
  println("A1x"+A1[0]+"**B1x"+B1[0]+"**C1x"+C1[0]);
  println("A1y"+A1[1]+"**B1y"+B1[1]+"**C1y"+C1[1]);
  println();
  //1.find the needed vectors,lengths,degrees of the triangle
  float AB1[] = new float[] {B1[0]-A1[0],B1[1]-A1[1],0};
  println("AB1x"+AB1[0]+"**AB1y"+AB1[1]);
  float AB1L = DogaMakeUnitVector(AB1);
  println("AB1x"+AB1[0]+"**AB1y"+AB1[1]);
  println("AB1L"+AB1L);
  println();

  float AC1[] = new float[] {C1[0]-A1[0],C1[1]-A1[1],0};
  println("AC1x"+AC1[0]+"**AC1y"+AC1[1]);
  float AC1L = DogaMakeUnitVector(AC1);
  println("AC1x"+AC1[0]+"**AC1y"+AC1[1]);
  println("AC1L"+AC1L);
  println();

  float AP[] = new float[] {frp[0]-A1[0],frp[1]-A1[1],0};
  println("frp[0]"+frp[0]+"**frp[1]"+frp[1]);
  println("APx"+AP[0]+"**AP"+AP[1]);
  float APL = DogaMakeUnitVector(AP);
  println("APx"+AP[0]+"**AP"+AP[1]);
  println("APL"+APL);
  println();

  float cosAlfa = DogaDotProduct(AB1,AP);
  float sinAlfa = sqrt(1-cosAlfa*cosAlfa);
  float cosBeta = DogaDotProduct(AP,AC1);
  float sinBeta = sqrt(1-cosBeta*cosBeta);
  println("cosAlfa"+cosAlfa+"sinAlfa"+sinAlfa);
  println("cosBeta"+cosBeta+"sinBeta"+sinBeta);
  println();

  //2.find the lengths needed for finding coefficients
  float d1 = APL*sinBeta;
//  float d2 = APL*sinAlfa;
  float AB1mid = d1/(sinAlfa*cosBeta+cosAlfa*sinBeta);
//  float AC1mid = d2/(sinAlfa*cosBeta+cosAlfa*sinBeta);
  float AC1mid = APL*cosBeta - sqrt(AB1mid*AB1mid-d1*d1);
//  float AC1mid = sqrt(APL*APL + AB1mid*AB1mid - 2*APL*AB1mid*cosAlfa);
  println("d1"+d1);
//  println("d2"+d2);
  println("AB1mid"+AB1mid);
  println("AC1mid"+AC1mid);
  //3.find the coefficients
  float ABcoef=(AB1mid/AB1L);
  float ACcoef=(AC1mid/AC1L);
  println("ABcoef"+ABcoef+"**ACcoef"+ACcoef);
  println();
  
  //4.find the new point according to new triangles coordinates
  println("A2x"+A2[0]+"**B2x"+B2[0]+"**C1x"+C2[0]);
  println("A2y"+A2[1]+"**B2y"+B2[1]+"**C1y"+C2[1]);
  float AB2[] = new float[] {B2[0]-A2[0],B2[1]-A2[1],0};
  println("AB2x"+AB2[0]+"**AB2y"+AB2[1]);
  float AB2L = DogaMakeUnitVector(AB2);
  println("AB2x"+AB2[0]+"**AB2y"+AB2[1]);
  println("AB2L"+AB2L);
  println();
  
  float AC2[] = new float[] {C2[0]-A2[0],C2[1]-A2[1],0};
  println("AC2x"+AC2[0]+"**AC2y"+AC2[1]);
  float AC2L = DogaMakeUnitVector(AC2);
  println("AC2x"+AC2[0]+"**AC2y"+AC2[1]);
  println("AC2L"+AC2L);
  println();
  
  top[0] = A2[0] + ABcoef*AB2[0]*AB2L + ACcoef*AC2[0]*AC2L;
  top[1] = A2[1] + ABcoef*AB2[1]*AB2L + ACcoef*AC2[1]*AC2L;

  float frp20 = A1[0] + ABcoef*(B1[0]-A1[0]) + ACcoef*(C1[0]-A1[0]);
  float frp21 = A1[1] + ABcoef*AB1[1]*AB1L + ACcoef*AC1[1]*AC1L;
  
  println("frp[0]"+frp[0]+"**frp[1]"+frp[1]);
  println("frp20 "+frp20+"**frp21 "+frp21);
  println("top[0]"+top[0]+"**top[1]"+top[1]);
  println();
}

float DogaDotProduct(float V1[],float V2[]) {return V1[0]*V2[0]+V1[1]*V2[1]+V1[2]*V2[2];}
float DogaMakeUnitVector(float V[]) {
  float w=0;
  w = sqrt(DogaDotProduct(V,V));
  if (w!=0) {
    V[0] = V[0]/w;
    V[1] = V[1]/w;
    V[2] = V[2]/w;
  }
  else {
    V[0] = 0;
    V[1] = 0;
    V[2] = 0;
  }
  return w;
}
